{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Landsat Collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geetools import collection, tools, ui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipygee import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Default values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: {'process': 'RAW', 'sensor': 'MSS'},\n",
       " 2: {'process': 'RAW', 'sensor': 'MSS'},\n",
       " 3: {'process': 'RAW', 'sensor': 'MSS'},\n",
       " 4: {'process': 'SR', 'sensor': 'TM'},\n",
       " 5: {'process': 'SR', 'sensor': 'TM'},\n",
       " 7: {'process': 'SR', 'sensor': 'ETM'},\n",
       " 8: {'process': 'SR', 'sensor': 'OLI'}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.landsat.DEFAULTS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This means that the default collection for Landsat 1 is RAW and MSS, for Landsat 5 is SR and TM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HELPERS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_values(image):\n",
    "    print(tools.image.getValue(image, image.geometry().centroid(1), 30, side='client'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Landsat 8 SR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8sr = collection.Landsat(8, 'SR')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Information about the collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'algorithms': {'brdf': <bound method Landsat.brdf of <geetools.collection.landsat.Landsat object at 0x7f39f129fa90>>,\n",
       "  'harmonize': <bound method Landsat.harmonize of <geetools.collection.landsat.Landsat object at 0x7f39f129fa90>>,\n",
       "  'rescale': <bound method Landsat.rescaleAll of <geetools.collection.landsat.Landsat object at 0x7f39f129fa90>>},\n",
       " 'band_names': ['aerosol',\n",
       "  'blue',\n",
       "  'green',\n",
       "  'red',\n",
       "  'nir',\n",
       "  'swir',\n",
       "  'swir2',\n",
       "  'thermal',\n",
       "  'thermal2',\n",
       "  'sr_aerosol',\n",
       "  'pixel_qa',\n",
       "  'radsat_qa'],\n",
       " 'bands': ['B1',\n",
       "  'B2',\n",
       "  'B3',\n",
       "  'B4',\n",
       "  'B5',\n",
       "  'B6',\n",
       "  'B7',\n",
       "  'B10',\n",
       "  'B11',\n",
       "  'sr_aerosol',\n",
       "  'pixel_qa',\n",
       "  'radsat_qa'],\n",
       " 'bit_bands': ['sr_aerosol', 'pixel_qa', 'radsat_qa'],\n",
       " 'classification_bands': [],\n",
       " 'cloud_cover': 'CLOUD_COVER',\n",
       " 'ee_collection': <ee.imagecollection.ImageCollection at 0x7f39f127b8d0>,\n",
       " 'end_date': '2019-05-07',\n",
       " 'id': 'LANDSAT/LC08/C01/T1_SR',\n",
       " 'indices': {'evi': <function geetools.collection.Collection.evi(self, image, name='evi', renamed=False)>,\n",
       "  'nbr': <function geetools.collection.Collection.nbr(self, image, name='nbr', renamed=False)>,\n",
       "  'ndvi': <function geetools.collection.Collection.ndvi(self, image, name='ndvi', renamed=False)>},\n",
       " 'number': 8,\n",
       " 'optical_bands': ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'],\n",
       " 'process': 'SR',\n",
       " 'ranges': {'B1': {'max': 10000, 'min': 0},\n",
       "  'B10': {'max': 10000, 'min': 0},\n",
       "  'B11': {'max': 10000, 'min': 0},\n",
       "  'B2': {'max': 10000, 'min': 0},\n",
       "  'B3': {'max': 10000, 'min': 0},\n",
       "  'B4': {'max': 10000, 'min': 0},\n",
       "  'B5': {'max': 10000, 'min': 0},\n",
       "  'B6': {'max': 10000, 'min': 0},\n",
       "  'B7': {'max': 10000, 'min': 0},\n",
       "  'pixel_qa': {'max': None, 'min': None},\n",
       "  'radsat_qa': {'max': None, 'min': None},\n",
       "  'sr_aerosol': {'max': None, 'min': None}},\n",
       " 'scales': {'B1': 30,\n",
       "  'B10': 30,\n",
       "  'B11': 30,\n",
       "  'B2': 30,\n",
       "  'B3': 30,\n",
       "  'B4': 30,\n",
       "  'B5': 30,\n",
       "  'B6': 30,\n",
       "  'B7': 30,\n",
       "  'pixel_qa': 30,\n",
       "  'radsat_qa': 30,\n",
       "  'sr_aerosol': 30},\n",
       " 'sensor': 'OLI',\n",
       " 'spacecraft': 'LANDSAT',\n",
       " 'start_date': '2013-04-11',\n",
       " 'thermal_bands': ['B10', 'B11'],\n",
       " 'tier': 1}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.info(l8sr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get a band by id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8sr.getBand('atmos_opacity')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'aerosol'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l8sr.getBand('B1').name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'B4'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l8sr.getBand('red', 'name').id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get one image out of the collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8sr_i = ee.Image(l8sr.collection.first())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'B7': 3120, 'B10': 2639, 'pixel_qa': 480, 'B2': 8329, 'B5': 7478, 'B11': 2642, 'B6': 3754, 'B3': 8054, 'radsat_qa': 0, 'B4': 8031, 'sr_aerosol': 8, 'B1': 8394}\n"
     ]
    }
   ],
   "source": [
    "print_values(l8sr_i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Rename bands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['B1',\n",
       " 'B2',\n",
       " 'B3',\n",
       " 'B4',\n",
       " 'B5',\n",
       " 'B6',\n",
       " 'B7',\n",
       " 'B10',\n",
       " 'B11',\n",
       " 'sr_aerosol',\n",
       " 'pixel_qa',\n",
       " 'radsat_qa']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l8sr_i.bandNames().getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8sr_renamed = l8sr.rename(l8sr_i, 'all')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['aerosol',\n",
       " 'blue',\n",
       " 'green',\n",
       " 'red',\n",
       " 'nir',\n",
       " 'swir',\n",
       " 'swir2',\n",
       " 'thermal',\n",
       " 'thermal2',\n",
       " 'sr_aerosol',\n",
       " 'pixel_qa',\n",
       " 'radsat_qa']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l8sr_renamed.bandNames().getInfo()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add NDVI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8sr_i_ndvi = l8sr.ndvi(l8sr_i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'ndvi': -0.03156969811083914}\n"
     ]
    }
   ],
   "source": [
    "print_values(l8sr_i_ndvi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get Common Bands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8toa = collection.Landsat8TOA()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B1 aerosol\n",
      "B2 blue\n",
      "B3 green\n",
      "B4 red\n",
      "B5 nir\n",
      "B6 swir\n",
      "B7 swir2\n",
      "B8 pan\n",
      "B9 cirrus\n",
      "B10 thermal\n",
      "B11 thermal2\n",
      "BQA bqa\n"
     ]
    }
   ],
   "source": [
    "for band in l8toa.bands:\n",
    "    print(band.id, band.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['green',\n",
       " 'thermal',\n",
       " 'swir',\n",
       " 'nir',\n",
       " 'aerosol',\n",
       " 'thermal2',\n",
       " 'red',\n",
       " 'blue',\n",
       " 'swir2']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.getCommonBands(l8sr, l8toa, reference='all', match='name')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get an Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8toa_i = ee.Image(l8toa.collection.first())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Re-scale to match Landsat 8 TOA collection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For rescaling uses `min` and `max` information for the band that match in each collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'B1': {'max': 10000, 'min': 0},\n",
       " 'B10': {'max': 10000, 'min': 0},\n",
       " 'B11': {'max': 10000, 'min': 0},\n",
       " 'B2': {'max': 10000, 'min': 0},\n",
       " 'B3': {'max': 10000, 'min': 0},\n",
       " 'B4': {'max': 10000, 'min': 0},\n",
       " 'B5': {'max': 10000, 'min': 0},\n",
       " 'B6': {'max': 10000, 'min': 0},\n",
       " 'B7': {'max': 10000, 'min': 0},\n",
       " 'pixel_qa': {'max': None, 'min': None},\n",
       " 'radsat_qa': {'max': None, 'min': None},\n",
       " 'sr_aerosol': {'max': None, 'min': None}}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# SR\n",
    "l8sr.ranges('all', False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'B1': {'max': 1, 'min': 0},\n",
       " 'B10': {'max': 1000, 'min': 0},\n",
       " 'B11': {'max': 1000, 'min': 0},\n",
       " 'B2': {'max': 1, 'min': 0},\n",
       " 'B3': {'max': 1, 'min': 0},\n",
       " 'B4': {'max': 1, 'min': 0},\n",
       " 'B5': {'max': 1, 'min': 0},\n",
       " 'B6': {'max': 1, 'min': 0},\n",
       " 'B7': {'max': 1, 'min': 0},\n",
       " 'B8': {'max': 1, 'min': 0},\n",
       " 'B9': {'max': 1, 'min': 0},\n",
       " 'BQA': {'max': None, 'min': None}}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TOA\n",
    "l8toa.ranges()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aerosol B1 int16\n",
      "blue B2 int16\n",
      "green B3 int16\n",
      "red B4 int16\n",
      "nir B5 int16\n",
      "swir B6 int16\n",
      "swir2 B7 int16\n",
      "thermal B10 int16\n",
      "thermal2 B11 int16\n",
      "sr_aerosol sr_aerosol uint8\n",
      "pixel_qa pixel_qa uint16\n",
      "radsat_qa radsat_qa uint16\n"
     ]
    }
   ],
   "source": [
    "for band in l8sr.bands:\n",
    "    print(band.name, band.id, band.precision)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'B7': 3132, 'B10': 2638, 'B9': 0.003796438919380307, 'B2': 7998, 'B5': 7538, 'B8': 0.7274518013000488, 'B11': 2642, 'B6': 3911, 'B3': 7182, 'B4': 7489, 'BQA': 2800, 'B1': 8138}\n"
     ]
    }
   ],
   "source": [
    "# l8_i_scaled_toa = l8sr.rescale_all(l8sr_i, 8, 'TOA', drop=True)\n",
    "l8_toa_scaled_sr = collection.rescale(l8toa_i, l8toa, l8sr, drop=False)\n",
    "print_values(l8_toa_scaled_sr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'B11': 2642, 'B6': 3911, 'B3': 7182, 'B10': 2638, 'B4': 7489, 'B5': 7538, 'B2': 7998, 'B7': 3132, 'B1': 8138}\n"
     ]
    }
   ],
   "source": [
    "l8_i_scaled_toa = collection.rescale(l8toa_i, l8toa, l8sr, drop=True)\n",
    "print_values(l8_i_scaled_toa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'nir': 0.7477999925613403, 'thermal': 263.8999938964844, 'aerosol': 0.8393999934196472, 'thermal2': 264.20001220703125, 'red': 0.8030999898910522, 'swir2': 0.31200000643730164, 'green': 0.805400013923645, 'blue': 0.8328999876976013, 'swir': 0.37540000677108765}\n"
     ]
    }
   ],
   "source": [
    "l8_renamed_scaled_toa = collection.rescale(l8sr_renamed, l8sr, l8toa, renamed=True, drop=True)\n",
    "print_values(l8_renamed_scaled_toa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pixel_qa': 480, 'sr_aerosol': 8, 'green': 0.805400013923645, 'thermal': 263.8999938964844, 'swir': 0.37540000677108765, 'nir': 0.7477999925613403, 'radsat_qa': 0, 'aerosol': 0.8393999934196472, 'thermal2': 264.20001220703125, 'red': 0.8030999898910522, 'blue': 0.8328999876976013, 'swir2': 0.31200000643730164}\n"
     ]
    }
   ],
   "source": [
    "l8_renamed_scaled_toa = collection.rescale(l8sr_renamed, l8sr, l8toa, renamed=True, drop=False)\n",
    "print_values(l8_renamed_scaled_toa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Re-scale a collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "col = l8sr.collection.limit(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_renamed = col.map(lambda img: l8sr.rename(img))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_reescaled = col_renamed.map(lambda img: collection.rescale(img, l8sr, l8toa, drop=False, renamed=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LC08_001004_20140524': {'aerosol': 0.8393999934196472,\n",
       "  'blue': 0.8328999876976013,\n",
       "  'green': 0.805400013923645,\n",
       "  'nir': 0.7477999925613403,\n",
       "  'pixel_qa': 480.0,\n",
       "  'radsat_qa': 0.0,\n",
       "  'red': 0.8030999898910522,\n",
       "  'sr_aerosol': 8.0,\n",
       "  'swir': 0.37540000677108765,\n",
       "  'swir2': 0.31200000643730164,\n",
       "  'thermal': 263.8999938964844,\n",
       "  'thermal2': 264.20001220703125},\n",
       " 'LC08_001004_20140609': {'aerosol': 0.5275999903678894,\n",
       "  'blue': 0.5042999982833862,\n",
       "  'green': 0.4708999991416931,\n",
       "  'nir': 0.4036000072956085,\n",
       "  'pixel_qa': 480.0,\n",
       "  'radsat_qa': 0.0,\n",
       "  'red': 0.45190000534057617,\n",
       "  'sr_aerosol': 8.0,\n",
       "  'swir': 0.08799999952316284,\n",
       "  'swir2': 0.09019999951124191,\n",
       "  'thermal': 260.79998779296875,\n",
       "  'thermal2': 260.8999938964844},\n",
       " 'LC08_001004_20140625': {'aerosol': 0.42309999465942383,\n",
       "  'blue': 0.4203000068664551,\n",
       "  'green': 0.41110000014305115,\n",
       "  'nir': 0.41990000009536743,\n",
       "  'pixel_qa': 480.0,\n",
       "  'radsat_qa': 0.0,\n",
       "  'red': 0.4133000075817108,\n",
       "  'sr_aerosol': 8.0,\n",
       "  'swir': 0.39320001006126404,\n",
       "  'swir2': 0.350600004196167,\n",
       "  'thermal': 268.20001220703125,\n",
       "  'thermal2': 268.79998779296875},\n",
       " 'LC08_001004_20140711': {'aerosol': 0.847100019454956,\n",
       "  'blue': 0.8565999865531921,\n",
       "  'green': 0.792900025844574,\n",
       "  'nir': 0.5672000050544739,\n",
       "  'pixel_qa': 336.0,\n",
       "  'radsat_qa': 0.0,\n",
       "  'red': 0.7488999962806702,\n",
       "  'sr_aerosol': 196.0,\n",
       "  'swir': 0.002099999925121665,\n",
       "  'swir2': 0.008799999952316284,\n",
       "  'thermal': 272.3999938964844,\n",
       "  'thermal2': 271.70001220703125},\n",
       " 'LC08_001004_20140727': {'aerosol': 0.5976999998092651,\n",
       "  'blue': 0.5936999917030334,\n",
       "  'green': 0.5773000121116638,\n",
       "  'nir': 0.5364999771118164,\n",
       "  'pixel_qa': 992.0,\n",
       "  'radsat_qa': 0.0,\n",
       "  'red': 0.5677000284194946,\n",
       "  'sr_aerosol': 8.0,\n",
       "  'swir': 0.18240000307559967,\n",
       "  'swir2': 0.19460000097751617,\n",
       "  'thermal': 250.5,\n",
       "  'thermal2': 251.39999389648438}}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.imagecollection.getValues(col_reescaled, ee.Image(col.first()).geometry().centroid(), 30, side='client')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# From Id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "l5toa = collection.Landsat.fromId('LANDSAT/LT05/C01/T1_TOA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'algorithms': {'brdf': <bound method Landsat.brdf of <geetools.collection.landsat.Landsat object at 0x7f39f11d5c88>>,\n",
       "  'harmonize': <bound method Landsat.harmonize of <geetools.collection.landsat.Landsat object at 0x7f39f11d5c88>>,\n",
       "  'rescale': <bound method Landsat.rescaleAll of <geetools.collection.landsat.Landsat object at 0x7f39f11d5c88>>},\n",
       " 'band_names': ['blue',\n",
       "  'green',\n",
       "  'red',\n",
       "  'nir',\n",
       "  'swir',\n",
       "  'thermal',\n",
       "  'swir2',\n",
       "  'bqa'],\n",
       " 'bands': ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'BQA'],\n",
       " 'bit_bands': ['BQA'],\n",
       " 'classification_bands': [],\n",
       " 'cloud_cover': 'CLOUD_COVER',\n",
       " 'ee_collection': <ee.imagecollection.ImageCollection at 0x7f39f11d5b70>,\n",
       " 'end_date': '2012-05-05',\n",
       " 'id': 'LANDSAT/LT05/C01/T1_TOA',\n",
       " 'indices': {'evi': <function geetools.collection.Collection.evi(self, image, name='evi', renamed=False)>,\n",
       "  'nbr': <function geetools.collection.Collection.nbr(self, image, name='nbr', renamed=False)>,\n",
       "  'ndvi': <function geetools.collection.Collection.ndvi(self, image, name='ndvi', renamed=False)>},\n",
       " 'number': 5,\n",
       " 'optical_bands': ['B1', 'B2', 'B3', 'B4', 'B5', 'B7'],\n",
       " 'process': 'TOA',\n",
       " 'ranges': {'B1': {'max': 1, 'min': 0},\n",
       "  'B2': {'max': 1, 'min': 0},\n",
       "  'B3': {'max': 1, 'min': 0},\n",
       "  'B4': {'max': 1, 'min': 0},\n",
       "  'B5': {'max': 1, 'min': 0},\n",
       "  'B6': {'max': 1, 'min': 0},\n",
       "  'B7': {'max': 1, 'min': 0},\n",
       "  'BQA': {'max': None, 'min': None}},\n",
       " 'scales': {'B1': 30,\n",
       "  'B2': 30,\n",
       "  'B3': 30,\n",
       "  'B4': 30,\n",
       "  'B5': 30,\n",
       "  'B6': 30,\n",
       "  'B7': 30,\n",
       "  'BQA': 30},\n",
       " 'sensor': 'TM',\n",
       " 'spacecraft': 'LANDSAT',\n",
       " 'start_date': '1984-01-01',\n",
       " 'thermal_bands': ['B6'],\n",
       " 'tier': 1}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.info(l5toa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'red'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l5toa.getBand('B3').name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = Map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2b318a7d4d9e4b9fa5ccdc4c1350da45",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(basemap={'max_zoom': 19, 'attribution': 'Map data (c) <a href=\"https://openstreetmap.org\">OpenStreetMap</a…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0b6beb4b007e41868c23e8f27e9bd8ed",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(CustomInspector(children=(SelectMultiple(options=OrderedDict(), value=()), Accordion(selected_in…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Map.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "rect = ee.Geometry.Rectangle([-71.57, -40.81, -71.43, -40.72])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(rect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = ee.Geometry.Point([-71.63085937500001, -40.76448635693034])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "l8sri = l8sr.collection.filterBounds(p).filterMetadata(l8sr.cloud_cover, 'less_than', 30).first()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "vis = l8sr.visualization('NSR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bands': ['B5', 'B6', 'B4'],\n",
       " 'max': [5000.0, 5000.0, 5000.0],\n",
       " 'min': [0, 0, 0]}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(l8sri, vis, 'L8 SR NSR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.centerObject(l8sri)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "proxy = l8sr.proxyImage()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "726c11a1af4a4ef1b0640aefc6a782b8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Accordion(children=(Output(),)),))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eprint(proxy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'pixel_qa': ['cloud',\n",
       "  'water',\n",
       "  'high_confidence_cirrus',\n",
       "  'clear',\n",
       "  'high_confidence_cloud',\n",
       "  'snow',\n",
       "  'shadow'],\n",
       " 'radsat_qa': ['B1_saturated',\n",
       "  'B2_saturated',\n",
       "  'B3_saturated',\n",
       "  'B4_saturated',\n",
       "  'B5_saturated',\n",
       "  'B6_saturated',\n",
       "  'B7_saturated',\n",
       "  'B9_saturated',\n",
       "  'B10_saturated',\n",
       "  'B11_saturated'],\n",
       " 'sr_aerosol': ['climatology', 'low', 'medium', 'high', 'water']}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l8sr.bitOptions()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "masked = l8sr.applyMask(l8sri, 'pixel_qa', ['cloud', 'snow', 'shadow'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(masked, vis, 'Masked L8 SR (pixel-qa)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
